package com.example.work02;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnInsert,btnDelete,btnUpdate,btnFindByName,btnFindAll;
    TextView tvMessage;
    EditText etName,etPhone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        btnInsert = findViewById(R.id.btn_insert);
        btnDelete = findViewById(R.id.btn_delete);
        btnUpdate = findViewById(R.id.btn_update);
        btnFindByName = findViewById(R.id.btn_findby_name);
        btnFindAll = findViewById(R.id.btn_find_all);
        etName = findViewById(R.id.et_name);
        etPhone = findViewById(R.id.et_phone);

        //为点击事件设置监听器
        btnInsert.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnFindByName.setOnClickListener(this);
        btnFindAll.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //设置点击事件
        switch (v.getId()){
            case R.id.btn_insert:
                add();
                findAll();
                break;
            case R.id.btn_delete:
            {
                del();
                findAll();
            }
                break;
            case R.id.btn_update:
                update();
                findAll();
                break;
            case R.id.btn_findby_name:
                findByName();
                break;
            case R.id.btn_find_all:
                findAll();
                break;
        }
    }
    //根据名字查询
    private void findByName() {
        String name = etName.getText().toString();
        String phone = etPhone.getText().toString();
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this,
                "contactdb",
                null,
                1);
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        Cursor cursor = readableDatabase.query("contact",null,"name=?", new String[]{name},null,null,null);
        StringBuffer sb = new StringBuffer();
        while (cursor.moveToNext()){
            sb.append("ID:"+cursor.getInt(0)+"\t"+"name:"+cursor.getString(1)+"\t"+"tel:"+cursor.getString(2));
        }
        readableDatabase.close();

        ((TextView)findViewById(R.id.tv_message)).setText(sb.toString());
        Toast.makeText(this, "查询数据成功", Toast.LENGTH_SHORT).show();
    }
    //根据name修改数据
    private void update() {
        String name = etName.getText().toString();
        String phone = etPhone.getText().toString();
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this,
                "contactdb",
                null,
                1);
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("phone",phone);
        writableDatabase.update("contact",values,"name=?", new String[]{name});
        writableDatabase.close();
        Toast.makeText(this, "更新数据成功", Toast.LENGTH_SHORT).show();
    }
    //根据name删除数据
    private void del() {
        //创建数据访问对象
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this,
                "contactdb",
                null,
                1);
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        String name = ((EditText)findViewById(R.id.et_name)).getText().toString();
        values.put("name",name);
        //删除记录
        writableDatabase.delete("contact", "name=?", new String[]{name});
        Toast.makeText(this, "删除数据成功", Toast.LENGTH_SHORT).show();
    }
    //查询所有
    private void findAll() {
        //创建数据访问对象
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this,
                "contactdb",
                null,
                1);
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();

        Cursor cursor = readableDatabase.query("contact",null,null,null,null,null,null);
        StringBuffer sb = new StringBuffer();
        while (cursor.moveToNext()){
            sb.append("ID:"+cursor.getInt(0)+"\t"+"name:"+cursor.getString(1)+"\t"+"tel:"+cursor.getString(2)+"\n");
        }
        readableDatabase.close();
        //查询所有记录
        ((TextView)findViewById(R.id.tv_message)).setText(sb.toString());
        Toast.makeText(this, "查询全部数据成功", Toast.LENGTH_SHORT).show();
    }
    //添加数据
    private void add() {
        //创建数据访问对象
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this,
                "contactdb",
                null,
                1);
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        String name = etName.getText().toString();
        String phone = etPhone.getText().toString();
        values.put("_id", (byte[]) null);
        values.put("name",name);
        values.put("phone",phone);
        //添加记录
        writableDatabase.insert("contact",null,values);
        Toast.makeText(this, "添加数据成功", Toast.LENGTH_SHORT).show();
    }
}